package com.gitee.baqkft.code.model;

import java.util.List;

import javax.swing.table.AbstractTableModel;

import com.gitee.baqkft.code.db.DBColumn;

/**
 * 数据表结构模型
 * @author chenmingcai
 * @date 2018年5月19日 下午1:40:59 
 *
 */

public class DataTableModel extends AbstractTableModel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final String[] columnNames = {
			"列名", "类型", "长度", "精度", "主键", "默认值", "备注"
	};
	
	/**
	 * 数据表列
	 */
	private List<DBColumn> columns;

	public DataTableModel(List<DBColumn> columns)
	{
		super();
		this.columns = columns;
	}

	@Override
	public int getRowCount()
	{
		if(columns == null)
		{
			return 0;
		}
		return columns.size();
	}

	@Override
	public int getColumnCount()
	{
		return columnNames.length;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex)
	{
		if(columns == null)
		{
			return null;
		}
		
		DBColumn col = columns.get(rowIndex);
		String[] datas = {
				col.getName(), col.getType(), String.valueOf(col.getLength()), 
				String.valueOf(col.getPrecision()), String.valueOf(col.isPrimary()),
				col.getDefaultValue(), col.getComment()
		};
		return datas[columnIndex];
	}

	@Override
	public String getColumnName(int column)
	{
		return columnNames[column];
	}

}
